from DBA.dbmanager import DBInterface
from SalleDAC import Salle
class Batiment:
	
	def __init__(self,id=""):
		self.id			= {"key":"ID","value":id}
		self.name		= {"key":"nom","value":""}
		self.description	= {"key":"description","value":""}
		self.xposition		= {"key":"x","value":""}
		self.yposition		= {"key":"y","value":""}
		self.zposition		= {"key":"z","value":""}
		self.height		= {"key":"realheight","value":""}
		self.width		= {"key":"realwidth","value":""}
		self.urlPlan		= {"key":"url_plan","value":""}
		self.ensSalles		= []
		self.__interfacebase	= DBInterface()
		if not id:
			self.id["value"]= str(self.__interfacebase.createNewID());
	
	def createByDico(self,dico):
		self.name["value"] 		= dico[self.name["key"]]
		self.description["value"] 	= dico[self.description["key"]]
		self.xposition["value"] 	= dico[self.xposition["key"]]
		self.yposition["value"] 	= dico[self.yposition["key"]]
		self.zposition["value"] 	= dico[self.zposition["key"]]
		self.height["value"] 		= dico[self.height["key"]]
		self.width["value"] 		= dico[self.width["key"]]
		self.urlPlan["value"] 		= dico[self.urlPlan["key"]]

	def addSalle(self,salle):
		self.ensSalles.append(salle)
	
	def nbSalles(self):
		return self.ensSalles.count()
	
	def selectBatimentFromDB(self):
	      return self.__interfacebase.selectTable(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping)

	def selectSallesFromDB(self):
		key ={}
		key["link_ba"] = self.id["value"]
		self.ensSalles = self.__interfacebase.selectTable(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,key)
		return self.ensSalles

	def selectAccessPointFromDB(self):
		self.selectSallesFromDB()
		ensAP = []
		for salle in self.ensSalles:
			s = Salle(salle["ID"])
			s.createByDico(salle)
			list = s.selectAccessPointFromDB()
			for AP in list:
				ensAP.append(AP)
		return ensAP

	def insertToDb(self):
		dico = self.createBatimentDico()
		self.__interfacebase.insert(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping,dico)
		for salle in self.ensSalles:
			salle.insertToDb(self.id["value"])
		

	def updateIntoDb(self):
		dico = self.createBatimentDico()
		self.__interfacebase.update(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping,dico)
		for salle in self.ensSalles:
			salle.updateIntoDb(self.id["value"])
	
	def createBatimentDico(self):
		dico ={}
		dico[self.id["key"]]			= self.id["value"]
		dico[self.name["key"]]			= self.name["value"]
		dico[self.description["key"]]		= self.description["value"]
		dico[self.xposition["key"]]		= self.xposition["value"]
		dico[self.yposition["key"]]		= self.yposition["value"]
		dico[self.zposition["key"]]		= self.zposition["value"]
		dico[self.height["key"]]		= self.height["value"]
		dico[self.width["key"]]			= self.width["value"]
		dico[self.urlPlan["key"]]		= self.urlPlan["value"]
		return dico
